<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>老师管理系统</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            margin: 0;
            padding: 20px;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .logo img {
            width: 80px;
            height: auto;
        }
        .user-info {
            display: flex;
            align-items: center;
        }
        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            margin-right: 10px;
        }
        .btn-add {
            background: #2196F3;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }
        .btn-add:hover {
            background: #0b7dda;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
        }
        th {
            background: #f2f2f2;
        }
        .btn-edit, .btn-delete {
            padding: 5px 10px;
            border-radius: 3px;
            color: white;
            text-decoration: none;
            margin: 0 5px;
        }
        .btn-edit {
            background: #FFC107;
        }
        .btn-delete {
            background: #F44336;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 100;
            justify-content: center;
            align-items: center;
        }
        .modal-content {
            background: white;
            padding: 20px;
            border-radius: 10px;
            width: 400px;
        }
        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        .modal-title {
            font-size: 20px;
            font-weight: bold;
        }
        .close {
            font-size: 24px;
            cursor: pointer;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 3px;
        }
        .modal-footer {
            text-align: right;
        }
        .btn-save, .btn-cancel {
            padding: 5px 15px;
            border-radius: 3px;
            border: none;
            cursor: pointer;
            margin-left: 10px;
        }
        .btn-save {
            background: #4CAF50;
            color: white;
        }
        .btn-cancel {
            background: #ddd;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="logo">
            <img src="{{ url_for('static', filename='images/logo.png') }}" alt="系统Logo">
        </div>
        <div class="user-info">
            <img src="{{ url_for('static', filename='images/user_default.png') }}" alt="用户头像" class="user-avatar">
            <span>欢迎，管理员</span>
        </div>
    </div>
    <h2>老师信息管理</h2>
    <a href="javascript:;" class="btn-add" id="btnAddTeacher">新增老师</a>

    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>学科</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="teacherTableBody">
            <!-- 老师列表由JS动态渲染 -->
        </tbody>
    </table>

    <!-- 新增/编辑老师模态框 -->
    <div class="modal" id="teacherModal">
        <div class="modal-content">
            <div class="modal-header">
                <div class="modal-title" id="teacherModalTitle">新增老师</div>
                <div class="close" id="closeTeacherModal">&times;</div>
            </div>
            <form id="teacherForm">
                <input type="hidden" id="teacherId">
                <div class="form-group">
                    <label for="teacherName">姓名</label>
                    <input type="text" id="teacherName" required>
                </div>
                <div class="form-group">
                    <label for="teacherAge">年龄</label>
                    <input type="number" id="teacherAge">
                </div>
                <div class="form-group">
                    <label for="teacherSubject">学科</label>
                    <input type="text" id="teacherSubject">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn-cancel" id="cancelTeacher">取消</button>
                    <button type="submit" class="btn-save" id="saveTeacher">保存</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 页面加载完成后获取老师列表
        window.onload = function() {
            fetchTeacherList();
        };

        // 获取老师列表
        function fetchTeacherList() {
            fetch('/teacher/teacher', {
                method: 'GET'
            })
            .then(response => response.json())
            .then(data => {
                const tbody = document.getElementById('teacherTableBody');
                tbody.innerHTML = '';
                for (const id in data.data) {
                    const teacher = data.data[id];
                    const tr = document.createElement('tr');
                    tr.innerHTML = `
                        <td>${id}</td>
                        <td>${teacher.name}</td>
                        <td>${teacher.age || '未知'}</td>
                        <td>${teacher.subject || '未知'}</td>
                        <td>
                            <a href="javascript:;" class="btn-edit" onclick="editTeacher(${id})">编辑</a>
                            <a href="javascript:;" class="btn-delete" onclick="deleteTeacher(${id})">删除</a>
                        </td>
                    `;
                    tbody.appendChild(tr);
                }
            })
            .catch(error => console.error('获取老师列表失败:', error));
        }

        // 新增老师
        document.getElementById('btnAddTeacher').addEventListener('click', function() {
            document.getElementById('teacherId').value = '';
            document.getElementById('teacherModalTitle').textContent = '新增老师';
            document.getElementById('teacherForm').reset();
            document.getElementById('teacherModal').style.display = 'flex';
        });

        // 编辑老师
        function editTeacher(id) {
            fetch(`/teacher/teacher/${id}`, {
                method: 'GET'
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('teacherId').value = id;
                document.getElementById('teacherModalTitle').textContent = '编辑老师';
                document.getElementById('teacherName').value = data.data.name;
                document.getElementById('teacherAge').value = data.data.age;
                document.getElementById('teacherSubject').value = data.data.subject;
                document.getElementById('teacherModal').style.display = 'flex';
            })
            .catch(error => console.error('获取老师信息失败:', error));
        }

        // 删除老师
        function deleteTeacher(id) {
            if (confirm('确定要删除该老师吗？')) {
                fetch(`/teacher/teacher/${id}`, {
                    method: 'DELETE'
                })
                .then(response => response.json())
                .then(data => {
                    alert(data.message);
                    fetchTeacherList();
                })
                .catch(error => console.error('删除老师失败:', error));
            }
        }

        // 保存老师（新增/编辑）
        document.getElementById('teacherForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const id = document.getElementById('teacherId').value;
            const name = document.getElementById('teacherName').value;
            const age = document.getElementById('teacherAge').value || null;
            const subject = document.getElementById('teacherSubject').value || null;

            const data = { name };
            if (age !== null) data.age = age;
            if (subject !== null) data.subject = subject;

            const method = id ? 'PUT' : 'POST';
            const url = id ? `/teacher/teacher/${id}` : '/teacher/teacher';

            fetch(url, {
                method: method,
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                document.getElementById('teacherModal').style.display = 'none';
                fetchTeacherList();
            })
            .catch(error => console.error('保存老师失败:', error));
        });

        // 关闭模态框
        document.getElementById('closeTeacherModal').addEventListener('click', function() {
            document.getElementById('teacherModal').style.display = 'none';
        });
        document.getElementById('cancelTeacher').addEventListener('click', function() {
            document.getElementById('teacherModal').style.display = 'none';
        });
    </script>
</body>
</html>